package com.thread.sync.sync08;

import java.util.concurrent.BlockingQueue;

public class Consume implements Runnable {
    //共享缓存区
    private BlockingQueue<Data> queue;

    public Consume(BlockingQueue<Data> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                //从共享队列中获取数据    take方法:如果queue为空,则线程阻塞一直等待,直到queue中有数据为止
                Data data = this.queue.take();
                //处理数据
                Thread.sleep(1000);
                System.out.println("当前消费线程：" + Thread.currentThread().getName() + "， 消费成功，消费数据为id: " + data.getId());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
